<?php
require __DIR__ . '/bootstrap.php';
$em = require __DIR__ . '/example/em.php';

use Service\UserManager;
use Auth\Auth;

require 'auth.php';

// в коде...
if (isset($_POST['login_name']) and isset($_POST['login_password'])) {
    $auth = new Auth($em);
    $login = $_POST['login_name'];
    $password = md5($_POST['login_password']);

    if ($auth->login($login, $password)) {
        // Прошел
        echo '1';
        exit();
    } else {
        echo '0';
        exit();
    }
}
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="initial-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
    <link rel="icon" type="image/ico" href="favicon.ico">
    <title>Вход</title>
    <link rel="stylesheet" href="template/css/login.css">
    <!-- jQuery framework -->
    <script src="template/js/jquery.min.js"></script>
    <!-- custom stylesheet -->
    <link rel="stylesheet" href="template/css/custom.css">
    <!-- validation -->
    <script src="template/js/lib/jquery-validation/jquery.validate.js"></script>
    <!-- sticky notifications -->
    <link rel="stylesheet" href="template/js/lib/sticky/sticky.css">
    <!-- sticky notifications -->
    <script src="template/js/lib/sticky/sticky.min.js"></script>

    <script src="template/js/jquery.form.js"></script>

    <script src="template/js/jquery.jrumble.1.3.js"></script>


    <script type="text/javascript">
        (function (a) {
            a.fn.vAlign = function () {
                return this.each(function () {
                    var b = a(this).height(), c = a(this).outerHeight(), b = (b + (c - b)) / 2;
                    a(this).css("margin-top", "-" + b + "px");
                    a(this).css("top", "50%");
                    a(this).css("position", "absolute")
                })
            }
        })(jQuery);
        (function (a) {
            a.fn.hAlign = function () {
                return this.each(function () {
                    var b = a(this).width(), c = a(this).outerWidth(), b = (b + (c - b)) / 2;
                    a(this).css("margin-left", "-" + b + "px");
                    a(this).css("left", "50%");
                    a(this).css("position", "absolute")
                })
            }
        })(jQuery);
        function showResponse(responseText, statusText, xhr, $form) {
            //colnsole.log(responseText);
            $.sticky(responseText.message, {autoclose: 3000, position: "top-center", type: responseText.type});
            $('.panel:visible').slideUp('200', function () {
                $('.panel').not($(this)).slideDown('200');
            });
            $(this).children('span').toggle();
        }
        $(document).ready(function () {

            $('#login-validate').ajaxForm({
                // dataType identifies the expected content type of the server response
                dataType: 'json',

                // success identifies the function to invoke when the server response
                // has been received
                success: processJson
            });
            function processJson(data) {
                // 'data' is the json object returned from the server
                //alert(data.message);
                if (data == 1) {
                    window.location.replace("index.php");
                } else {
                    $.sticky("Неправильный логин или пароль!", {autoclose: 3000, position: "top-center", type: "st-error"});
                    $('#jrumble').jrumble({
                        x: 10,
                        y: 10,
                        rotation: 4
                    });
                    var demoTimeout;
                    $this = $('#jrumble');
                    clearTimeout(demoTimeout);
                    $this.trigger('startRumble');
                    demoTimeout = setTimeout(function () {
                        $this.trigger('stopRumble');
                    }, 1000);
                }
            }


            if ($('#login-wrapper').length) {
                $("#login-wrapper").vAlign().hAlign()
            }
            ;
            if ($('#login-validate').length) {
                $('#login-validate').validate({
                    onkeyup: false,
                    errorClass: 'error',
                    rules: {
                        login_name: {required: true},
                        login_password: {required: true}
                    }
                })
            }
            if ($('#forgot-validate').length) {
                $('#forgot-validate').validate({
                    onkeyup: false,
                    errorClass: 'error',
                    rules: {
                        forgot_email: {required: true, email: true}
                    }
                })
            }
            $('#pass_login').click(function () {
                $('.panel:visible').slideUp('200', function () {
                    $('.panel').not($(this)).slideDown('200');
                });
                $(this).children('span').toggle();
            });
            var options = {
                //target:        '#output2',   // target element(s) to be updated with server response 
                //beforeSubmit:  showRequest,  // pre-submit callback 
                success: showResponse,  // post-submit callback

                // other available options: 
                //url:       url         // override for form's 'action' attribute 
                //type:      type        // 'get' or 'post', override for form's 'method' attribute 
                dataType: "json",         // 'xml', 'script', or 'json' (expected server response type) 
                clearForm: true        // clear all form fields after successful submit 
                //resetForm: true        // reset the form after successful submit 

                // $.ajax options can be used here too, for example: 
                //timeout:   3000 
            };
            $('#forgot-validate').submit(function () {
                if ($("#forgot_email").val() < 6) {
                    return false;
                }
                // inside event callbacks 'this' is the DOM element so we first 
                // wrap it in a jQuery object and then invoke ajaxSubmit 
                $(this).ajaxSubmit(options);

                // !!! Important !!! 
                // always return false to prevent standard browser submit and page navigation 
                return false;
            });
        });
    </script>
</head>
<body>
<div id="login-wrapper" class="clearfix">
    <div id="jrumble">
        <div class="main-col">
            <img src="template/img/beoro.png" alt="" class="logo_img"/>

            <div class="panel">
                <p class="heading_main">Вход в систему</p>

                <form id="login-validate" method="post">
                    <label for="login_name">Логин</label>
                    <input type="text" id="login_name" name="login_name" value=""/>
                    <label for="login_password">Пароль</label>
                    <input type="password" id="login_password" name="login_password" value=""/>

                    <div class="submit_sect">
                        <button id="submit" type="submit" class="btn btn-beoro-3">Вход</button>
                    </div>
                </form>
            </div>
            <div class="panel" style="display:none">
                <p class="heading_main">Забыли пароль?</p>

                <form id="forgot-validate" method="post" action="mail.php">
                    <label for="forgot_login">Укажите логин</label>
                    <input type="text" id="forgot_login" name="forgot_login" value=""/>

                    <label for="forgot_email">Укажите email</label>
                    <input type="text" id="forgot_email" name="forgot_email" value=""/>

                    <div class="submit_sect">
                        <button id="send_pass" type="submit" class="btn btn-beoro-3">Выслать пароль</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="login_links">
        <a href="javascript:void(0)" id="pass_login"><span>Забыли пароль?</span><span
                style="display:none">Вход</span></a>
    </div>
</div>
</body>
</html>